﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Text;

namespace EasyIot.WinApp
{
    public class PostHelper
    { 
        public static PostResult HttpPost(string url, string param)
        {
            PostResult ret = new PostResult();

            try
            {
                WebClientEx client = new WebClientEx();
                client.Timeout = 6000;
                client.Encoding = Encoding.UTF8;
                client.Headers.Add("Content-Type", "application/json");
                string jsonBack = client.UploadString(url, "POST", param); 
                JObject jsonInfo = (JObject)JsonConvert.DeserializeObject(jsonBack); 
                if (jsonBack.Contains("code"))
                {
                    ret.Code = jsonInfo.Value<int>("code");
                }
                if (jsonBack.Contains("message"))
                {
                    ret.Message = jsonInfo.Value<string>("message");
                }
                if (jsonBack.Contains("result"))
                {
                    ret.Result = jsonInfo.Value<string>("result");
                    if (!string.IsNullOrEmpty(ret.Result)) ret.Result = DESEncrypt.Decrypt(ret.Result);
                }
            }
            catch (Exception ex)
            {
                ret.Message = ex.Message;
            } 

            return ret;
        }
    }
}
